// pages/shopshare/shopshare.js

const common = require('../../mixins/common')
const options = {
  /**
   * 页面的初始数据
   */
  data: {
    historySearchList: [],
    show: true,
    hotlist:['口罩','外套女韩版宽松学生','零食','水果','儿童玩具','男鞋'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let historySearchList = wx.getStorageSync('historySearchList');
    if (historySearchList) {
      this.setData({
        historySearchList: historySearchList,
      })
    }
  },

  clearHistory: function() {
    let keywords = {};
    wx.setStorageSync('historySearchList', keywords);
    let historySearchList = wx.getStorageSync('historySearchList');
    if (historySearchList) {
      this.setData({
        historySearchList: historySearchList,
      })
    }
  },


  /**
   * 监听输入
   */

  onSearchInput: function(e) {
    this.setData({
      keyword: e.detail.value,
      show: false
    })
  },

  
  /**
   * 确定搜索
   */

  onSearchConfirm: function() {
    this.setData({
      isLastPage: false,
      posts: [],
      page: 1,
      show: false,
    })
    this.getSearchPosts({
      keyword: this.data.keyword,
      page: this.data.page
    })
    let readsave = true
    // 过滤重复值
    if (this.data.historySearchList.length > 0) {
      for (let i = 0; i < this.data.historySearchList.length; i++) {
        if (this.data.historySearchList[i] == this.data.keyword) {
          readsave = false
          break;
        }
      }

    }
    if (readsave) {
      this.data.historySearchList.push(this.data.keyword);
      this.setData({

        historySearchList: this.data.historySearchList
      })
      wx.setStorageSync('historySearchList', this.data.historySearchList);
    }

  },

  
  chooseHistory: function(e) {
    let keyword = e.currentTarget.dataset.keyword
    this.setData({
      isLastPage: false,
      posts: [],
      page: 1,
      show: false,
      keyword: keyword
    })
    this.getSearchPosts({
      keyword: keyword,
      page: this.data.page
    })
    let readsave = true
    // 过滤重复值
    if (this.data.historySearchList.length > 0) {
      for (let i = 0; i < this.data.historySearchList.length; i++) {
        if (this.data.historySearchList[i] == this.data.keyword) {
          readsave = false
          break;
        }
      }

    }
    if (readsave) {
      this.data.historySearchList.push(this.data.keyword);
      this.setData({

        historySearchList: this.data.historySearchList
      })
      wx.setStorageSync('historySearchList', this.data.historySearchList);
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isBottom: true,
    })
    if (!this.data.isLastPage) {
      this.getSearchPosts({
        keyword: this.data.keyword,
        page: this.data.page
      })
    } else {
      wx.showToast({
        title: '最后一页啦',
      })
    }
  },

  
  goodsDetails: function(e) {
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/shop/pddetail?id=' + id,
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}

common(options)
Page(options)
